﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using CheckBox = System.Windows.Forms.CheckBox;

namespace fingerWinForm.GUI
{
    public partial class ManageUsersWindow : Form
    {
        private int rowChecked;
        private int lastCheckedBox;
        public ManageUsersWindow()
        {
            InitializeComponent();
            loadData();
            lastCheckedBox = 0;
        }

        public void loadData()
        {
            XmlDataDocument xmlData = new XmlDataDocument();
            xmlData.DataSet.ReadXml(MainWindow.startingPath + "Users.xml");
            dataGridView.DataSource = xmlData.DataSet;
            dataGridView.DataMember = "user";
        }

        private void removeButton_Click(object sender, EventArgs e)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(MainWindow.startingPath + "Users.xml");
            XmlNode rootNode = xmlDoc.SelectSingleNode("//users");
            XmlNodeList usersList = rootNode.SelectNodes("user");
            try
            {
                rootNode.RemoveChild(usersList[rowChecked]);
            }
            catch (Exception exception)
            {
                MessageBox.Show("Please choose a row with user data","Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
                return;
            }
            xmlDoc.Save(MainWindow.startingPath + "Users.xml");
            loadData();
        }

        private void dataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            rowChecked = e.RowIndex;
            if (dataGridView.Rows[lastCheckedBox].Cells[0].Value != null && (bool)dataGridView.Rows[lastCheckedBox].Cells[0].Value)
            {
                dataGridView.Rows[lastCheckedBox].Cells[0].Value = false;
            }
            lastCheckedBox = rowChecked;

        }

        private void addButton_Click(object sender, EventArgs e)
        {
            AddUserWindow addUserWindow = new AddUserWindow();
            addUserWindow.ShowDialog();
            loadData();
        }

        private void closeButton_Click(object sender, EventArgs e)
        {
            Close();
        }


    }
}
